package main

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/olivere/elastic/v7"
)

type Account struct {
	Name    string    `json:"name"`
	Address string    `json:"address"`
	Age     int       `json:"age"`
	Company []Company `json:"company"`
}

type Company struct {
	Name    string `json:"name"`
	Address string `json:"address"`
}

func main() {
	url := "http://120.24.67.46:9200"
	client, err := elastic.NewClient(elastic.SetURL(url), elastic.SetSniff(false))
	if err != nil {
		// Handle error
		panic(err)
	}

	q := elastic.NewMatchQuery("address", "西湖")
	res, err := client.Search().Index("account").Query(q).Do(context.Background())
	if err != nil {
		// Handle error
		panic(err)
	}

	for _, value := range res.Hits.Hits {
		var account Account
		_ = json.Unmarshal(value.Source, &account)
		fmt.Println("account.Name", account.Name)
		fmt.Println("account.Address", account.Address)
		fmt.Println("account.Age", account.Age)
		fmt.Println("account.Company.Name", account.Company)
	}
}
